package com.example.apione.interceptor;



import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * @author mryunqi
 * {@code @date} 2023/1/9
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle( HttpServletRequest request,  HttpServletResponse response,  Object handler) throws Exception {
        Cookie[] cookie = request.getCookies();
        String username= (String) request.getSession().getAttribute("username");
        if (cookie!=null){
            for (Cookie cookie1:cookie) {
                if (cookie1.getValue().equals(username)){
//                    System.out.println(cookie1.getValue());
                    return true;
                }
            }
            response.sendRedirect("/login");
            return false;
        }else {
            response.sendRedirect("/login");
            return false;
        }




    }
}
